import Mock from "mockjs";

let { data } = Mock.mock({
  "data|120": [
    {
      type: "租房",
      "num|+1": 1,
      "time|1640966400000-1670947200000":1640966400000,
      name: "@cname",
      "sex|1": ["男", "女"],
      "phone|13000000000-18999999999": 13000000000,
      "paytype|1": ["微信支付", "支付宝支付", "银行卡支付", "现金支付"],
      money: "+1,200",
    },
  ],
});

Mock.mock("/api/ordertotal", "post", (options) => {
  
  const { pageSize, currentPage, search } = JSON.parse(options.body);
  if (pageSize && currentPage) {
    let result;
    let count = 0;
    if (search) {
      result = data
        .filter((item: any) => item.num + "" === search)
        .slice((currentPage - 1) * pageSize, currentPage * pageSize);
      count = data.filter((item: any) => item.num === search).length;
    } else {
      result = data.slice((currentPage - 1) * pageSize, currentPage * pageSize);
      count = data.length;
    }
    return {
      status: 0,
      result: result,
      count: count,
    };
  } else {
    return {
      status: 1,
      result: "参数错误",
    };
  }
});


Mock.mock("/api/ordertotal/del","post",(options)=>{
  const {num}=JSON.parse(options.body);
  if(num){
  data=data.filter((item:any)=>item.num!==num)
    return {
      status:0,
      msg:"删除成功"
    }
  }else{
    return {
      status:1,
      msg:"参数错误"
    }
  }
})

Mock.mock("/api/ordertotal/add", "post", (options) => {
  const {  type, name,money } = JSON.parse(
    options.body
  );
  
  if (type && name && money) {
  data.unshift( JSON.parse( options.body))
  return{
    status:0,
  msg:"添加成功"
  }
  }else{
    return{
      status:1,
      msg:"参数错误,必须有姓名、收入和订单类型数据"
    }
  }
});
